package com.red.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.red.constant.MessageConstant;
import com.red.context.BaseContext;
import com.red.dto.AttractionsStatusDTO;
import com.red.dto.HomePageQueryDTO;
import com.red.entity.*;
import com.red.exception.BaseException;
import com.red.mapper.AttractionsMapper;
import com.red.mapper.CarouselsMapper;
import com.red.mapper.UsersAttractionsMapper;
import com.red.result.PageResult;
import com.red.service.AttractionsService;
import com.red.service.ProductsService;
import com.red.service.TravelNotesService;
import com.red.vo.AttractionsVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 刘英豪
 */
@Service
public class AttractionsServiceImpl extends ServiceImpl<AttractionsMapper, Attractions> implements AttractionsService {

    @Autowired
    private AttractionsMapper attractionsMapper;
    @Autowired
    private CarouselsMapper carouselsMapper;
    @Autowired
    private TravelNotesService travelNotesService;
    @Autowired
    private ProductsService productsService;
    @Autowired
    private UsersAttractionsMapper usersAttractionsMapper;

    /**
     * 获取轮播图全部信息
     * @return
     */
    public List<Carousels> getCarouselsAll() {
        QueryWrapper<Carousels> qw = new QueryWrapper<>();
        return carouselsMapper.selectList(qw);
    }

    /**
     * 分页查询
     * @param dto
     * @return
     */
    public PageResult PageQuery(HomePageQueryDTO dto) {

        String type = dto.getType();
        //判断type

        //小记分页查询
        if (Objects.equals(type, "notes")) {
            return travelNotesService.pageQuery(dto);
        }
        //商品分页查询
        else if (Objects.equals(type, "product")) {
            return productsService.pageQuery(dto);
        }
        //景点分页查询
        //开始分页查询
        PageHelper.startPage(dto.getPage(),dto.getPageSize());

        Page<Attractions> page = attractionsMapper.pageQuery(dto);

        List<Attractions> result = page.getResult();

        ArrayList<AttractionsVO> objects = new ArrayList<>();



        for (Attractions attractions : result) {
            //判断是否去过
            QueryWrapper<UsersAttravtions> qw = new QueryWrapper<>();
            qw.eq("user_id",BaseContext.getCurrentId());
            AttractionsVO attractionsVO = new AttractionsVO();
            BeanUtils.copyProperties(attractions,attractionsVO);
            qw.eq("attractions_id",attractions.getId());
            UsersAttravtions usersAttravtions = usersAttractionsMapper.selectOne(qw);
            if (usersAttravtions==null){
                attractionsVO.setStatus(false);
            }else {
                attractionsVO.setStatus(true);
            }
            objects.add(attractionsVO);
        }

        return new PageResult(page.getTotal(),objects);
    }

    /**
     * 新增景点信息
     * @param attractions
     */
    public void insert(Attractions attractions) {

        attractionsMapper.insert(attractions);


    }

    /**
     * 根据ID景点删除
     * @param id
     */
    @Transactional
    public void deleteById(Long id) {

        attractionsMapper.deleteById(id);

        QueryWrapper<UsersAttravtions> qw = new QueryWrapper<>();
        qw.eq("attractions_id",id);
        usersAttractionsMapper.delete(qw);
    }

    /**
     * 根据ID修改景点信息
     * @param attractions
     */
    public void updateAttraction(Attractions attractions) {
        attractionsMapper.updateById(attractions);

    }

    /**
     * 景点全部信息
     * @return
     */
    public List<Attractions> getScenicAll() {
        return attractionsMapper.selectList(new QueryWrapper<>());
    }

    /**
     * 是否去过该景点
     * @param dto
     */
    public void is_Status(AttractionsStatusDTO dto) {

        UsersAttravtions usersAttravtions = new UsersAttravtions();
        usersAttravtions.setUserId(dto.getUserId());
        usersAttravtions.setAttractionsId(dto.getAttractionId());

        QueryWrapper<UsersAttravtions> qw = new QueryWrapper<>();
        qw.eq("attractions_id",dto.getAttractionId());
        //判断status状态
        if (dto.getStatus()==1){
            //表示点击去过
            usersAttractionsMapper.insert(usersAttravtions);
        }else if (dto.getStatus()==0){
            //表示没有去过
            qw.eq("user_id",dto.getUserId());
            usersAttractionsMapper.delete(qw);
        }
        else {
            new BaseException(MessageConstant.UNKNOWN_ERROR);
        }
    }


    /**
     * 根据ID查询景点
     * @param id
     * @return
     */
    public AttractionsVO getAttractionById(Long id) {
        AttractionsVO attractionsVO = new AttractionsVO();

        Attractions attractions = attractionsMapper.selectById(id);
        BeanUtils.copyProperties(attractions,attractionsVO);

        //判断是否去过
        QueryWrapper<UsersAttravtions> qw = new QueryWrapper<>();
        qw.eq("user_id",BaseContext.getCurrentId());
        qw.eq("attractions_id",id);
        UsersAttravtions usersAttravtions = usersAttractionsMapper.selectOne(qw);
        if (usersAttravtions==null){
            attractionsVO.setStatus(false);
        }else {
            attractionsVO.setStatus(true);
        }
        return attractionsVO;
    }

    /**
     * 根据用户ID查询去过的景点
     * @return
     */
    public List<Attractions> selectByUserId() {
        HashMap<String , Object> map = new HashMap<>();
        Long userId = BaseContext.getCurrentId();


        map.put("user_id",userId);
        List<UsersAttravtions> usersAttravtions = usersAttractionsMapper.selectByMap(map);

        if (usersAttravtions==null || usersAttravtions.isEmpty()){
            //如果为空直接给个空对象
            return new ArrayList<Attractions>();
        }else {
            //如果不为空查找所有用户去过的景点
            List<Long> attractionsIds = usersAttravtions.stream().map(UsersAttravtions::getAttractionsId).collect(Collectors.toList());
            return attractionsMapper.selectByIds(attractionsIds);
        }

    }

    @Override
    public PageResult AdminpageQuery(HomePageQueryDTO dto) {
        //景点分页查询
        //开始分页查询
        PageHelper.startPage(dto.getPage(),dto.getPageSize());

        Page<Attractions> page = attractionsMapper.pageQuery(dto);

        return new PageResult(page.getTotal(),page.getResult());
    }
}
